package com.javasm.controller.goodsController;

import com.github.pagehelper.PageInfo;
import com.javasm.common.http.AxiosResult;
import com.javasm.common.page.PageResult;
import com.javasm.controller.base.BaseController;
import com.javasm.domin.criteria.goodsCriteria.GdTypeCriteria;
import com.javasm.domin.entity.goodsEntity.GdBrand;
import com.javasm.domin.entity.goodsEntity.GdType;
import com.javasm.domin.vo.goodsVo.GdTypeVo;
import com.javasm.service.goodsService.GdGoodsService;
import com.javasm.service.goodsService.GdTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：zzt
 * @Version：1.0
 * @Date：2021/11/19-11:29
 * @Since:jdk1.8
 * @Description:
 */
@RestController
@RequestMapping("/type")
@RequiredArgsConstructor
public class GdTypeController extends BaseController {

    @Autowired
    private final GdTypeService gdTypeService;
    @Autowired
    private final GdGoodsService gdGoodsService;

    /**
     * 查所有
     * @param
     * @return
     */
    @GetMapping("list")
    public AxiosResult<List<GdTypeVo>> list(){

//        List<GdType> list = gdTypeService.list();
        List<GdTypeVo> gdTypeVos = gdTypeService.selectByPid();
        System.out.println(gdTypeVos.toString());
        return AxiosResult.success(gdTypeVos);
    }

    @GetMapping("findList")
    public AxiosResult<PageResult<GdTypeVo>> findList(GdTypeCriteria gdTypeCriteria){
        PageResult<GdTypeVo> gdTypeVoPageResult = gdTypeService.selectCriteria(gdTypeCriteria);
        return AxiosResult.success(gdTypeVoPageResult);
    }


    @PostMapping("addType")
    public AxiosResult addType(@RequestBody GdType gdType){

        return AxiosResult.success(gdTypeService.save(gdType));

    }


    @PutMapping("updateType")
    public AxiosResult updateType(@RequestBody GdType gdType){

        int i = gdTypeService.updateType(gdType.getId().longValue());
        if (i>0){
            gdType.setId(800000000);
            return AxiosResult.success(gdTypeService.updateById(gdType));
        }else {
            return AxiosResult.success(gdTypeService.updateById(gdType));
        }



    }


    @DeleteMapping("deleteById/{id}")
    public AxiosResult deleteById(@PathVariable Long id){


        int i = gdTypeService.selectPid(id);

        if (i>0){
            return AxiosResult.success(gdTypeService.removeById(800000));
        }else {
            return AxiosResult.success(gdTypeService.removeById(id));
        }

//        if (gdGoodsService.selectTypeId(id)==0){
//            System.out.println("==========="+gdGoodsService.selectTypeId(id));
//            return AxiosResult.success(gdTypeService.removeById(id));
//        }
    }


    @DeleteMapping("batchDeleteByIds/{ids}")
    public AxiosResult batchDeleteByIds(@PathVariable List<Long> ids){

        int i = gdTypeService.batchDeleteByIds(ids);

        if (i>0){
            return toAxios(10);
        }else {
            return toAxios(gdTypeService.batchDeleteByIds(ids));
        }



//        return null;
    }



    @GetMapping("/tree")
    public AxiosResult<List<GdTypeVo>> getTree(){
        List<GdTypeVo> allTypeTree = gdTypeService.getAllTypeTree();
        return AxiosResult.success(allTypeTree);
    }



    @GetMapping("/findById/{id}")
    public AxiosResult<GdType> findById(@PathVariable Long id){

        GdType byId = gdTypeService.getById(id);

        return AxiosResult.success(byId);

    }





}
